package com.scenery.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class HttpUtil {
	private final static String TAG = HttpUtil.class.getSimpleName();
	/**
	 * 检查网络是否已经连接
	 * @param context
	 * @return
	 */
	public static boolean isNetWorkConnected(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectivityManager.getActiveNetworkInfo();
		if (info != null && info.isConnected()) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 
	 * @param url
	 *            客户端请求地址
	 * @return 从服务器返回XML请求信息
	 */
	public static String httpRequestInfo(String url) {

		String result = "";
		Log.d(TAG, "客户端发送的请求地址:" + url);
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);

		ResponseHandler<String> responseHandler = new BasicResponseHandler();

		try {
			result = httpclient.execute(httpget, responseHandler);

			Log.d(TAG, ">>服务器返回的结果信息" );
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			Log.e(TAG, "ClientProtocolException客户端获取HTTP请求信息出错\n" + e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			Log.e(TAG, "IOException客户端获取HTTP请求信息出错\n" + e.getMessage());
		}

		return result;
	}
	public static String httpPost(ArrayList<NameValuePair> parameters,String url )
	{
		
		Log.d(TAG, "httpPost");
		Log.d(TAG, "url:"+url);
		StringBuilder responseData =new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(parameters, HTTP.UTF_8));
			HttpResponse resp =client.execute(httpPost);
			if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				System.err.println("HttpPost Method failed: "
						+ resp.getStatusLine());
				httpPost.abort();
			}else 
			{
				
				HttpEntity entity = resp.getEntity();
				if (entity != null) {
					BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8192);
	    			String line = null;
	    			while ((line = reader.readLine())!= null){
	    				responseData.append(line + "\n");
	    			}
	    			reader.close();
				}
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			Log.d(TAG, "UnsupportedEncodingException");
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			Log.d(TAG, "ClientProtocolException");
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d(TAG, "IOException");
			e.printStackTrace();
		}
		 
		return responseData.toString();
	}
}
